package ui.activity.home.presenter;


import com.google.gson.Gson;

import javax.inject.Inject;

import Bean.ProductBean;
import Bean.ProductParamBean;
import Utils.StringUtil;
import base.BaseBiz;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import ui.activity.home.biz.NewProductBiz;
import ui.activity.home.contract.NewProductContract;
import ui.activity.order.OrderAct;

/**
 * Created by chenjian on 19/2/26
 */

public class NewProductPresenter implements NewProductContract.Presenter {

    NewProductContract.View view;
    NewProductBiz productBiz;
    int buyNumber = 1;
    String totalMoney;
    int number = 0;
    ProductBean.DataBean bean;


    @Inject
    public NewProductPresenter(NewProductContract.View view) {
        this.view = view;
    }


    @Override
    public void setBiz(BaseBiz biz) {
        productBiz = (NewProductBiz) biz;
    }


    @Override
    public void getPosModel(boolean isRefresh, String id) {
        view.loading(true);
        ProductParamBean productParamBean = new ProductParamBean();
        productParamBean.setId(id);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), new Gson().toJson(productParamBean));
        productBiz.getPosModel(requestBody, new BaseBiz.Callback<ProductBean.DataBean>() {
            @Override
            public void onSuccess(ProductBean.DataBean info) {
                totalMoney = StringUtil.getFormatPrice(info.getItem().getPrice());
                number = info.getItem().getNumber();
                bean = info;
                view.upDateUI(info);
                view.hasLoadMore(false);
                if (isRefresh) {
                    view.refreshComplete();
                } else {
                    view.loadMoreComplete();
                }
                view.loading(false);
            }

            @Override
            public void onFailure(ProductBean.DataBean info) {
                view.loading(false);
                if (isRefresh) {
                    view.refreshComplete();
                } else {
                    view.loadMoreComplete();
                }
            }
        });

    }

    @Override
    public void toOrder() {
        view.startAct(OrderAct.class, null);
    }

    @Override
    public void toSubmitOrder() {
        view.submitOrder(buyNumber, totalMoney);
    }

    @Override
    public void toNumber(int type) {
        if (type == 0) {
            buyNumber--;
        } else if (type == 1) {
            buyNumber++;
        }
        if (buyNumber == 0) {
            buyNumber = 1;
        }
        view.upDateNumber(buyNumber);
    }

    @Override
    public void toInput(String numberInfo) {
        if (bean == null) {
            return;
        }
        try {
            if (StringUtil.isNullOrEmpty(numberInfo)||"0".equals(numberInfo)) {
                buyNumber = 1;
            } else {
                buyNumber = Integer.parseInt(numberInfo);
            }
            String dayStringInfo = "";
            if (bean.getRules().size() > 0) {
                for (int i = 0; i < bean.getRules().size(); i++) {
                    if (bean.getRules().get(i).getMaxCount() != -1) {
                        if (buyNumber * number > bean.getRules().get(i).getMinCount() && buyNumber * number <= bean.getRules().get(i).getMaxCount()) {
                            dayStringInfo = "（有效期" + bean.getRules().get(i).getExpireTime() + "天）";
                        }
                    } else {
                        if (buyNumber * number >= bean.getRules().get(i).getMinCount()) {
                            dayStringInfo = "（有效期" + bean.getRules().get(i).getExpireTime() + "天）";
                        }
                    }
                }
            } else {
                dayStringInfo = "（有效期" + bean.getItem().getUntilDay() + "天）";
            }
            double totalPrice = buyNumber * bean.getItem().getPrice();
            totalMoney = StringUtil.getFormatPrice(totalPrice);
            view.updatePrice(buyNumber, totalPrice+"", dayStringInfo);
        } catch (Exception e) {

        }
    }

}
